<template>
  <vxe-modal
    title="查看加计扣除成果详情"
    v-model="dialogVisible"
    v-if="dialogVisible"
    :loading="loading"
    width="70%" showFooter esc-closable show-zoom resize>

    <div style="height: 69vh; overflow-y: auto;">
      <el-card class="box-card" shadow="never" :body-style="{ padding: '10px',backgroundColor: '#bfcdda' }">
        <!-- 基本资料 -->
        <el-card class="box-card" shadow="hover">
          <div slot="header" class="clearfix">
            <span style="font-size: 16px; color: #1890ff;">基本资料</span>
          </div>
          <el-descriptions class="margin-top" :column="2" size="medium" border
                           :contentStyle="content_style" :labelStyle="label_style">
            <el-descriptions-item>
              <template slot="label">
                <div class="right-align-label">
                  <i class="el-icon-user"></i>
                  年度：
                </div>
              </template>
              {{ jjkccgData.nd }}
            </el-descriptions-item>
            <el-descriptions-item>
              <template slot="label">
                <div class="right-align-label">
                  <i class="el-icon-user"></i>
                  归集总额(万元)：
                </div>
              </template>
              {{ jjkccgData.gjZe }}
            </el-descriptions-item>
            <el-descriptions-item>
              <template slot="label">
                <div class="right-align-label">
                  <i class="el-icon-user"></i>
                  国税认定额(万元)：
                </div>
              </template>
              {{ jjkccgData.gsRde }}
            </el-descriptions-item>
            <el-descriptions-item>
              <template slot="label">
                <div class="right-align-label">
                  <i class="el-icon-user"></i>
                  所得税抵扣额(万元)：
                </div>
              </template>
              {{ jjkccgData.sdsDke }}
            </el-descriptions-item>

          </el-descriptions>
        </el-card>
        <br>
        <!-- 相关附件 -->
        <el-card class="box-card" shadow="hover">
          <div slot="header" class="clearfix">
            <span style="font-size: 16px; color: #1890ff">相关附件</span>
          </div>
          <el-row :gutter="20">
            <el-col :span="4" :xs="24">
              <div class="head-container">
                <el-tree
                  :data="fileOptions"
                  :props="defaultProps"
                  :expand-on-click-node="false"
                  :filter-node-method="filterNode"
                  ref="tree"
                  node-key="id"
                  default-expand-all
                  highlight-current
                  @node-click="handleNodeClick"
                  :render-content="renderContent"
                />
              </div>
            </el-col>
            <el-col :span="20" :xs="24">
              <el-table :header-cell-style="{background:'#d0cece',color:'#000000',fontWeight:'bold'}"
                        :data="jjkccgFjList" height="35vh" v-loading="loadingFj" ref="jjkccgFj">
                <el-table-column type="selection" width="50" align="center"/>
                <el-table-column label="序号" align="center" type="index" width="50"/>
                <el-table-column label="文件类型" align="center" prop="fileType"/>
                <el-table-column label="文件名称" align="center" prop="fileName" :formatter="getFileNames"/>
                <el-table-column label="文件大小" align="center" prop="fileSize"/>
                <el-table-column width="120" align="center" label="操作" class-name="small-padding fixed-width">
                  <template slot-scope="scope">
                    <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-view"
                      @click="handlePreview(scope.row)"
                    >预览
                    </el-button>
                    <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-edit"
                      @click="handleDownload(scope.row)"
                    >下载
                    </el-button>
                  </template>
                </el-table-column>
              </el-table>
            </el-col>
          </el-row>
        </el-card>
        <br>

      </el-card>
    </div>

    <div slot="footer" class="dialog-footer dialog-footer-center">
      <el-button @click="dialogVisible=false">关闭</el-button>
    </div>

    <!-- 预览组件 -->
    <KkFileView ref="preview"/>
  </vxe-modal>
</template>
<script>

import {getJjkccg, selectJjkccgFjList} from "@/api/jjkc/jjkccg";
import KkFileView from "@/views/components/KKFileView/kkFileView.vue";

export default {
  name: "JjKcCgXq",
  components: {KkFileView},
  //加计扣除成果详情组件
  data() {
    return {
      loadingFj: false,
      jjkccgFileType: "鉴定资料", //默认的附件类型
      fileOptions: [{ //附件类型树表
        id: 1,
        label: '1.鉴定资料(word)',
        type: '鉴定资料'
      }, {
        id: 2,
        label: '2.国税资料(word)',
        type: '国税资料'
      }],
      defaultProps: {children: "children", label: "label"}, // 表单参数
      // 加计扣除成果附件表格数据
      jjkccgFjList: [],
      loading: false,
      jjkccgData: [], //加计扣除成果基本资料
      jjkccgId: null, //加计扣除成果主键ID
      dialogVisible: false,
      content_style: {
        // 解决不对齐的问题
        'width': "400px",
        // 排列第二行
        'word-break': 'break-all',
        // 'background-color': '#f3f7ff'
      },
      label_style: {
        // 解决不对齐的问题
        'width': "250px",
        // 排列第二行
        'word-break': 'break-all',
      },
    };
  },
  created() {
  },
  methods: {
    // 打开预览组件
    handlePreview(row) {
      this.$refs.preview.handleOpen(row);
    },
    handleNodeClicks(data) {
      this.loadingFj = true
      let jjkccgId = this.jjkccgId
      this.jjkccgFileType = data.type
      selectJjkccgFjList(jjkccgId, this.jjkccgFileType).then(resFileList => {
        this.jjkccgFjList = resFileList.data;
        this.loadingFj = false
      })
    },
    // 节点单击事件
    handleNodeClick(data, node) {
      this.loadingFj = true
      let jjkccgId = this.jjkccgId
      if (node.level > 0 && node.data.children && node.data.children.length > 0) { // 检查是否有子节点
        const firstChildNode = node.data.children[0]; // 获取第一个子节点
        this.$refs.tree.setCurrentKey(firstChildNode.id); // 设置当前选中的节点为第一个子节点
        data = firstChildNode; // 更新 data 为第一个子节点的数据
        this.jjkccgFileType = data.type
        selectJjkccgFjList(jjkccgId, this.jjkccgFileType).then(resFileList => {
          this.jjkccgFjList = resFileList.data;
          this.loadingFj = false
        })
      } else {
        //根据附件类型查询附件列表
        this.jjkccgFileType = data.type
        selectJjkccgFjList(jjkccgId, this.jjkccgFileType).then(resFileList => {
          this.jjkccgFjList = resFileList.data;
          this.loadingFj = false
        })
      }
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 文件下载处理
    handleDownload(row) {
      const resource = row.fileName;
      // 默认方法
      this.$download.resource(resource);
    },
    getFileNames(row) {
      // 从路径中提取文件名
      return row.fileName.split('/').pop(); // 返回文件名部分
    },
    handleOpen(row) {
      this.loading = true;
      this.jjkccgId = row.jjkccgId
      this.dialogVisible = true
      this.getJjKcCgData()
      setTimeout(() => {
        this.loading = false
      }, 1000)
    },
    //获取详情数据
    getJjKcCgData() {
      const jjkccgId = this.jjkccgId
      getJjkccg(jjkccgId).then(res => {
        this.jjkccgData = res.data

        selectJjkccgFjList(jjkccgId, this.jjkccgFileType).then(resFileList => {
          this.jjkccgFjList = resFileList.data;

          this.$nextTick(() => {
            const firstNode = this.$refs.tree.store.nodesMap['1']; // 默认点击id为1的节点
            if (firstNode) {
              this.$refs.tree.setCurrentKey(firstNode.key);
              this.handleNodeClicks(firstNode.data);
            }
          });

        })

      })
    },


  }
};
</script>

<style scoped>
.right-align-label {
  text-align: right;
}
</style>
